package com.sudao.android.candle;

import java.util.Map;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.util.Log;

import java.lang.Integer;

public class Setting extends PreferenceActivity 
		implements SharedPreferences.OnSharedPreferenceChangeListener {
    
	public static final String MULTI = "multi";
	public static final String SHAPE = "shape";
	public static final String COUNT = "count";
	
	public static final String HEART = "Heart shape";
	public static final String ILOVEU = "i love u";

	public static final boolean DEFAULT_MULTI = false;
	public static final String DEFAULT_SHAPE = HEART;
	public static final int DEFAULT_COUNT = 20;

	private Intent mIntent = new Intent();
	private Bundle bundle = new Bundle();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.settings);
		
        CheckBoxPreference checkPref = ((CheckBoxPreference) findPreference(MULTI));
        checkPref.setChecked(PreferenceManager.getDefaultSharedPreferences(this)
			.getBoolean(MULTI, DEFAULT_MULTI));
		bundle.putBoolean(MULTI, checkPref.isChecked());

		String[] titleList = {HEART, ILOVEU};
        String[] valueList = {HEART, ILOVEU};
        ListPreference shapePref = (ListPreference) findPreference(SHAPE);
        shapePref.setEntries(titleList);
       	shapePref.setEntryValues(valueList);
		String shape = PreferenceManager.getDefaultSharedPreferences(this).getString(SHAPE, DEFAULT_SHAPE);
        shapePref.setValue(shape);
        shapePref.setSummary(shapePref.getEntry());
		bundle.putString(SHAPE, shape);

        final SeekBarPreference countPref = (SeekBarPreference) findPreference(COUNT);
        //countPref.setEnabled(checkPref.isChecked());
		countPref.setMin(10);
		countPref.setMax(40);
		countPref.setListener(new SeekBarPreference.Callback(){
		    public int onSetInitialProgress() {
		    	int volume = PreferenceManager.getDefaultSharedPreferences(Setting.this).
		    		getInt(COUNT, DEFAULT_COUNT);
			    countPref.setSummary(volume + "");
				return volume;
		    }

		    public String onSetMessage() {
		    	return Setting.this.getResources().getString(R.string.current_candle_count);
		    }

			public void onSaveProgress(int value){
				PreferenceManager.getDefaultSharedPreferences(Setting.this).edit().putInt(COUNT, value).commit();
		    }
		});

		int volume = PreferenceManager.getDefaultSharedPreferences(this).getInt(COUNT, DEFAULT_COUNT);
		bundle.putInt(COUNT, volume);

		countPref.setSummary(volume + "");
		
		PreferenceManager.getDefaultSharedPreferences(this)
			.registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	protected void onDestroy() {
		PreferenceManager.getDefaultSharedPreferences(this)
			.unregisterOnSharedPreferenceChangeListener(this);
		super.onDestroy();
	}

	@Override
	public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, 
			Preference preference) {
		return super.onPreferenceTreeClick(preferenceScreen, preference);
	}
    
	public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
		if (SHAPE.equals(key)){
			String shape = preferences.getString(SHAPE, DEFAULT_SHAPE);
			bundle.putString(key, shape);
        	ListPreference shapePref = (ListPreference) findPreference(SHAPE);
        	shapePref.setSummary(shapePref.getEntry());
		} else if(MULTI.equals(key)){
			bundle.putBoolean(key, preferences.getBoolean(MULTI, DEFAULT_MULTI));
		} else if(COUNT.equals(key)){
        	SeekBarPreference countPref = (SeekBarPreference) findPreference(COUNT);
			int volume = preferences.getInt(COUNT, DEFAULT_COUNT);
			countPref.setSummary(volume + "");
			bundle.putInt(key, volume);
		}

		mIntent.putExtras(bundle);
		setResult(RESULT_OK, mIntent);
	}
}
